import optparse
import os
import sys

from oriolus.pith.renamer import version
from oriolus.pith.renamer.renamer import Renamer, RenamerError

def exit(parser):
    parser.print_help()
    parser.destroy()
    sys.exit(2)

def run():
    usage = """usage: oriren [options] path"""

    parser = optparse.OptionParser(usage=usage, version = "oriren %s" % version)
    
    parser.add_option("-o", "--output-path", dest = "output_path", help = "path to output directory")
    
    (options, args) = parser.parse_args()
    
    if len(args) != 1:
        exit(parser)
        
    if not os.path.exists(args[0]):
        print "Input file '%s' does not exists." % args[0]
        sys.exit(2)
    
    output_path = None
    if options.output_path:
        output_path = options.output_path

    renamer = Renamer()
    try:
        if output_path:
            if not os.path.exists(output_path):
                print "Output folder '%s' does not exists." % output_path
                sys.exit(2)
                
            renamer.rename(args[0], output_path)
        else:
            renamer.rename(args[0])
    except RenamerError, message:
        print "Renaming is failed. Cause: %s" % message
        sys.exit(2)

if __name__ == "__main__":
    run()
